package com.ruoyi.web.controller.system;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.web.controller.demo.domain.SysSchedulPaiRelate;
import com.ruoyi.web.controller.demo.domain.SysWzxUser;
import com.ruoyi.web.controller.mapper.SysWzxUserMapper;
import com.ruoyi.web.controller.service.ISysSchedulPaiRelateService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.controller.demo.domain.SysSchedulZhi;
import com.ruoyi.web.controller.service.ISysSchedulZhiService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * sys_schedul_zhiController
 * 
 * @author ruoyi
 * @date 2022-12-27
 */
@Controller
@RequestMapping("/system/zhi")
public class SysSchedulZhiController extends BaseController
{
    private String prefix = "system/zhi";
    @Autowired
    private SysWzxUserMapper sysWzxUserMapper;
    @Autowired
    private ISysSchedulZhiService sysSchedulZhiService;
    @Autowired
    private ISysSchedulPaiRelateService relate;
    @RequiresPermissions("system:zhi:view")
    @GetMapping()
    public String zhi()
    {
        return prefix + "/zhi";
    }

    /**
     * 查询sys_schedul_zhi列表
     */
    @RequiresPermissions("system:zhi:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysSchedulZhi sysSchedulZhi)
    {
        startPage();
        List<SysSchedulZhi> list = sysSchedulZhiService.selectSysSchedulZhiList(sysSchedulZhi);
        return getDataTable(list);
    }

    /**
     * 导出sys_schedul_zhi列表
     */
    @RequiresPermissions("system:zhi:export")
    @Log(title = "sys_schedul_zhi", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysSchedulZhi sysSchedulZhi)
    {
        List<SysSchedulZhi> list = sysSchedulZhiService.selectSysSchedulZhiList(sysSchedulZhi);
        ExcelUtil<SysSchedulZhi> util = new ExcelUtil<SysSchedulZhi>(SysSchedulZhi.class);
        return util.exportExcel(list, "sys_schedul_zhi数据");
    }

    /**
     * 新增sys_schedul_zhi
     */
    @GetMapping("/add")
    public String add(Model model) throws ParseException {
        SysUser sysUser = getSysUser();
        model.addAttribute("user",sysUser);
        List<SysWzxUser> sysWzxUsers = sysWzxUserMapper.selectSysWzxUserList(null);
        model.addAttribute("users",sysWzxUsers);
        List<SysSchedulPaiRelate> relates = relate.selectSysSchedulPaiRelateList(null);
        model.addAttribute("relates",relates);


        return prefix + "/add";
    }

    /**
     * 新增保存sys_schedul_zhi
     */
    @RequiresPermissions("system:zhi:add")
    @Log(title = "sys_schedul_zhi", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysSchedulZhi sysSchedulZhi)
    {
        return toAjax(sysSchedulZhiService.insertSysSchedulZhi(sysSchedulZhi));
    }

    /**
     * 修改sys_schedul_zhi
     */
    @RequiresPermissions("system:zhi:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        List<SysWzxUser> sysWzxUsers = sysWzxUserMapper.selectSysWzxUserList(null);
        mmap.put("users",sysWzxUsers);
        SysSchedulZhi sysSchedulZhi = sysSchedulZhiService.selectSysSchedulZhiById(id);
        mmap.put("sysSchedulZhi", sysSchedulZhi);
        return prefix + "/edit";
    }

    /**
     * 修改保存sys_schedul_zhi
     */
    @RequiresPermissions("system:zhi:edit")
    @Log(title = "sys_schedul_zhi", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysSchedulZhi sysSchedulZhi)
    {
        //同时修改排班关系表中的值班人.以时间为判断条件,update table set    where time =
        return toAjax(sysSchedulZhiService.updateSysSchedulZhi(sysSchedulZhi));
    }

    /**
     * 删除sys_schedul_zhi
     */
    @RequiresPermissions("system:zhi:remove")
    @Log(title = "sys_schedul_zhi", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysSchedulZhiService.deleteSysSchedulZhiByIds(ids));
    }
}
